/*************************************************************************
 *************************************************************************
 	 	 	 	 	 	 	M2SMA : Logger.cpp
 *************************************************************************
 *
 	 Copyright (C) 2011
 	TODO, Steven Costiou, France
 *
 **************************************************************************
 **************************************************************************
 *
	///TODO FILE COMMENT
 *
 **************************************************************************
 **************************************************************************
 *
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/
 *
 **************************************************************************
 **************************************************************************/


#include <sstream>
#include "Logger.h"

using namespace std;

Logger::Logger()
{
	initialize();
}

Logger::~Logger()
{
	delete TRC_APP;
	delete TRC_LAYOUT;
	delete TRC_CAT;
}

void Logger::TRACE_EVENT(int aPriority, const char* anEvent)
{
	TRC_CAT->log(aPriority, anEvent);
}

Logger::Logger(const Logger& aLogger) {}

void Logger::initialize()
{
	buildLogFileName();

	TRC_APP = new log4cpp::FileAppender(logFileName, logFileName);
	TRC_LAYOUT = new log4cpp::BasicLayout();
	TRC_APP->setLayout(TRC_LAYOUT);
	TRC_CAT = &(log4cpp::Category::getInstance("TRC_CAT"));
	TRC_CAT->setAdditivity(true);
	TRC_CAT->setAppender(TRC_APP);
}

void Logger::buildLogFileName()
{
	time_t rawtime;
	struct tm* timeinfo;

	time (&rawtime);
	timeinfo = localtime (&rawtime);

	stringstream str;
	str << LOG_DIRECTORY;
	str << timeinfo->tm_year << "_";
	str << timeinfo->tm_mon << "_";
	str << timeinfo->tm_mday << "_";
	str << timeinfo->tm_hour << "_";
	str << timeinfo->tm_min << "_";

	logFileName = str.str().c_str();
	cout << logFileName << endl;
}
